package com.erbantou.filemanage.controller;

import com.erbantou.filemanage.service.ResourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ResourceController 提供了基于视图的接口，用于文件资源的管理操作
 * 包括列出资源、上传文件夹、下载文件夹以及删除资源等功能
 */
@Controller
@RequiredArgsConstructor // Lombok 会自动生成一个包含所有 final 字段的构造函数
@Slf4j // Lombok 会自动生成一个包含日志记录器的实例
@RequestMapping("/resources")
public class ResourceController {

    private static final String OBJECT_NAMES = "objectNames";
    private static final String ERROR = "error";
    private final ResourceService resourceService;

    /**
     * 列出所有资源对象并显示在资源页面上
     *
     * @param model 用于向视图传递数据
     * @return 资源页面视图名称
     */
    @GetMapping
    public String listResources(Model model) {
        try {
            // 获取当前登录的用户信息
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            boolean isLoggedIn = authentication != null
                    && authentication.isAuthenticated()
                    && !authentication.getPrincipal().equals("anonymousUser");
            model.addAttribute("isLoggedIn", isLoggedIn);
            // 调用 ResourceService 中的方法获取资源对象列表
            List<String> objectNames = resourceService.listResources();
            model.addAttribute(OBJECT_NAMES, objectNames);

            return "resources";
        } catch (Exception e) {
            log.error("无法列出资源", e);
            model.addAttribute(ERROR, "无法列出资源");
        }
        return "resources";
    }

    /**
     * 上传文件夹及其包含的文件到 MinIO 存储
     *
     * @param folderName     文件夹名称
     * @param files          包含在文件夹中的文件列表
     * @param requestId      请求 ID
     * @param removeOldFiles 是否删除旧文件
     * @param model          用于向视图传递数据
     * @return 重定向到资源页面视图名称
     */
    @PostMapping("/uploadFolder")
    public String uploadFolder(@RequestParam(value = "folderName", required = false, defaultValue = "") String folderName,
                               @RequestParam("files") List<MultipartFile> files,
                               @RequestParam("requestId") String requestId,
                               @RequestParam(value = "removeOldFiles", required = false) Boolean removeOldFiles,
                               Model model) {
        try {
            // 调用 ResourceService 中的方法上传文件夹
            resourceService.uploadFolder(folderName, files, requestId, removeOldFiles);
        } catch (IllegalArgumentException e) {
            model.addAttribute(ERROR, e.getMessage());
            return "resources";
        } catch (Exception e) {
            log.error("上传文件夹失败", e);
            model.addAttribute(ERROR, "上传文件失败：" + e.getMessage());
            return "resources";
        }
        return "redirect:/resources";
    }
}
